/**
 *
 */
package com.naiterui.ehp.bs.doctor.dao.impl;

import com.naiterui.common.repository.page.Page;
import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.ehp.bp.domain.Doctor;
import com.naiterui.ehp.bp.domain.Login;
import com.naiterui.ehp.bs.doctor.dao.IDoctorDao;
import com.naiterui.ehp.bs.doctor.domain.DrCity;
import com.naiterui.ehp.bs.doctor.repository.IDoctorRepository;
import com.naiterui.ehp.bs.doctor.repository.IDrCityRepository;
import com.naiterui.ehp.bs.doctor.repository.ILoginRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description 概述
 */
@Component
public class AdDoctorDaoImpl implements IDoctorDao {

    @Autowired
    private IDoctorRepository doctorRepository;

    @Autowired
    private ILoginRepository loginRepository;

    @Autowired
    private IDrCityRepository drCityRepository;

    @Override
    public Doctor get(Long doctorId) {
        return doctorRepository.get(doctorId);
    }

    @Override
    public void update(Doctor doctor) {
        doctorRepository.update(doctor);
    }

    @Override
    public void saveOrUpdate(Doctor doctor) {
        if (doctor.getId() == null) {
            doctorRepository.save(doctor);
        } else {
            doctorRepository.update(doctor);
        }

    }

    @Override
    public Long save(Doctor doctor) {
        doctorRepository.save(doctor);
        return doctor.getId();
    }

    @Override
    public Doctor getDoctorByPhone(String phone) {
        List<Doctor> list = doctorRepository.findByPhone(phone);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Doctor getDoctorByUserId(Long doctorId) {
        List<Doctor> list = doctorRepository.findByUserId(doctorId);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Page<Doctor> findUnLoginByTime(Date start, Date end, int pageNo, int pageSize) {
        Pageable pageable = new PageParamVO(pageNo, pageSize).getPageable();
        org.springframework.data.domain.Page<Login> page = loginRepository.findByLastLoginTimeIsNullAndCreatedAtBetween(start, end, pageable);
        Set<Long> doctorIdSet = new HashSet<>();
        for (Login login : page.getContent()) {
            doctorIdSet.add(login.getId());
        }
        List<Doctor> list = doctorRepository.findByUserIdIn(doctorIdSet);

        Page<Doctor> result = new Page<>();
        result.setPageNo(pageNo);
        result.setPageSize(pageSize);
        result.setResult(list);
        result.setTotalCount(page.getTotalElements());
        return result;
    }

    /*
     * 获取医生map(id,实体)
     * IDoctorDao.getDoctorMap()
     */
    @Override
    public Map<Long, Doctor> getDoctorMap(Set<Long> doctorIds) {
        Map<Long, Doctor> resultMap = new HashMap<>();
        List<Doctor> doctorList = getDoctors(doctorIds);
        if (null != doctorList && !doctorList.isEmpty()) {
            for (Doctor doctor : doctorList) {
                resultMap.put(doctor.getId(), doctor);
            }
        }
        return resultMap;
    }

    @Override
    public List<Doctor> getDoctors(Collection<Long> doctorIds) {
        List<Doctor> doctorList = new ArrayList<>();
        if (null != doctorIds && !doctorIds.isEmpty()) {
            doctorList = doctorRepository.findByIdIn(doctorIds);
        }
        return doctorList;
    }

    @Override
    public Map<Long, Integer> getDrSpecialCity(String drIds) {
        Map<Long, Integer> result = new HashMap<>();
        List<Long> doctorIds = Arrays.stream(StringUtils.split(drIds, ","))
                .map(Long::parseLong).collect(Collectors.toList());
        List<DrCity> drCities = drCityRepository.findByDoctorIdIn(doctorIds);
        if (null != drCities && drCities.size() > 0) {
            for (DrCity obj : drCities) {
                Long doctorId = obj.getDoctorId();
                Integer cityId = obj.getCityId();
                result.put(doctorId, cityId);
            }
        }
        return result;
    }

}
